package cn.kduck.module.role.web;

import cn.kduck.core.service.Page;
import cn.kduck.core.service.ParamMap;
import cn.kduck.core.utils.RequestUtils;
import cn.kduck.core.web.annotation.ModelOperate;
import cn.kduck.core.web.annotation.ModelResource;
import cn.kduck.core.web.json.JsonObject;
import cn.kduck.core.web.json.JsonPageObject;
import cn.kduck.module.role.service.Role;
import cn.kduck.module.role.service.RoleObject;
import cn.kduck.module.role.service.RoleService;
import cn.kduck.security.principal.AuthUser;
import cn.kduck.security.principal.AuthUserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * LiuHG
 */
@RestController
@RequestMapping("/role")
@ModelResource("角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/add")
    @ModelOperate(name="添加角色")
    public JsonObject addRole(Role role){
        roleService.addRole(role);
        return JsonObject.SUCCESS;
    }

    @DeleteMapping("/delete")
    @ModelOperate(name="删除角色")
    public JsonObject deleteRole(String[] ids){
        roleService.deleteRole(ids);
        return JsonObject.SUCCESS;
    }

    @PutMapping("/update")
    @ModelOperate(name="修改角色")
    public JsonObject updateRole(Role role){
        roleService.updateRole(role);
        return JsonObject.SUCCESS;
    }

    @GetMapping("/get")
    @ModelOperate(name="角色详情")
    public JsonObject getUser(@RequestParam("roleId") String roleId){
        Role user = roleService.getRole(roleId);
        return new JsonObject(user);
    }

    @GetMapping("/list")
    @ModelOperate(name="角色列表查询")
    public JsonObject listUser( Page page, HttpServletRequest request){
        List<Role> result = roleService.listRole(RequestUtils.getParameterMap(request), page);
        return new JsonPageObject(page,result);
    }

    @PostMapping("/object/get/{roleId}")
    @ModelOperate(name="角色对象添加")
    public JsonObject addRoleObject(@PathVariable String roleId,
                                    @RequestParam("objectIds") String[] objectIds,
                                    @RequestParam("objectType") Integer objectType){
        roleService.addRoleObject(roleId,objectIds,objectType);
        return JsonObject.SUCCESS;
    }

    @DeleteMapping("/object/delete")
    @ModelOperate(name="删除角色对象")
    public JsonObject deleteRoleObject(String[] objectIds){
        roleService.deleteRoleObject(objectIds);
        return JsonObject.SUCCESS;
    }

    @GetMapping("/object/list")
    @ModelOperate(name="查询角色列表查询")
    public JsonObject listRoleObject(@RequestParam("roleId") String roleId,
                                     @RequestParam("objectType") Integer objectType,
                                     @RequestParam(name="objectName",required = false) String objectName,
                                     Page page){
        List<RoleObject> list = roleService.listRoleObject(roleId,objectType, ParamMap.create("objectName",objectName).toMap(),page);
        return new JsonPageObject(page,list);
    }

    @GetMapping("/object/list/forSelect")
//    @ModelOperate(name="查询角色列表查询（选择）")
    public JsonObject listRoleObjectForSelect(@RequestParam("roleId") String roleId,
                                     @RequestParam("objectType") Integer objectType,
                                     @RequestParam(name="objectName",required = false) String objectName,
                                     Page page){
        List<RoleObject> list = roleService.listUserForRoleObject(roleId,objectType, ParamMap.create("objectName",objectName).toMap(),page);
        return new JsonPageObject(page,list);
    }

    @GetMapping("/hasRole")
    public JsonObject hasRole(@RequestParam("roleCode") String roleCode){
        AuthUser authUser = AuthUserHolder.getAuthUser();
        String userId = authUser.getUserId();
        boolean hasRole = roleService.hasRole(userId,roleCode);
        return new JsonObject(hasRole);
    }
}
